package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateTime;
import cn.stylefeng.guns.modular.comm.entity.MoneyHistory;
import cn.stylefeng.guns.modular.comm.entity.WxMiniUser;
import cn.stylefeng.guns.modular.comm.model.params.WxMiniUserParam;
import cn.stylefeng.guns.modular.comm.service.MoneyHistoryService;
import cn.stylefeng.guns.modular.comm.service.WxMiniUserService;
import cn.stylefeng.guns.modular.ht.entity.BackRegister;
import cn.stylefeng.guns.modular.ht.model.params.BackRegisterParam;
import cn.stylefeng.guns.modular.ht.service.BackRegisterService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/wx/mini/user")
public class ApiNeedTokenWxMiniUserController extends BaseController {
    @Autowired
    private WxMiniUserService wxMiniUserService;
    @Autowired
    private MoneyHistoryService moneyHistoryService;

    /**
     * 绑定
     */
    @PostMapping(value = "add")
    public Object index(WxMiniUserParam wxMiniUserParam) {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();

        WxMiniUser wxMiniUser = wxMiniUserService.getOne(new QueryWrapper<WxMiniUser>().eq("open_id", wxMiniUserParam.getOpenId()));
        if (wxMiniUser == null) {
            wxMiniUserService.add(wxMiniUserParam);
        }else{
            //wxMiniUser.setName(wxMiniUserParam.getName());
            wxMiniUserService.updateById(wxMiniUser);
        }

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @GetMapping(value = "detail")
    public Object detail(WxMiniUserParam wxMiniUserParam) {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();

        WxMiniUser wxMiniUser = wxMiniUserService.getOne(new QueryWrapper<WxMiniUser>().eq("open_id", wxMiniUserParam.getOpenId()));
        map.put("wxMiniUser", wxMiniUser);

        QueryWrapper queryWrapper = new QueryWrapper<MoneyHistory>();
        queryWrapper.eq("open_id", wxMiniUserParam.getOpenId());
        queryWrapper.eq("type", "3");
        List<MoneyHistory> moneyHistories = moneyHistoryService.list(queryWrapper);
        BigDecimal sum = BigDecimal.ZERO;
        for(MoneyHistory moneyHistory:moneyHistories) {
            sum = sum.add(moneyHistory.getMoney());
        }
        map.put("sum", sum);
        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @PostMapping(value = "out")
    public Object out(WxMiniUserParam wxMiniUserParam) {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();

        WxMiniUser wxMiniUser = wxMiniUserService.getOne(new QueryWrapper<WxMiniUser>().eq("open_id", wxMiniUserParam.getOpenId()));
        wxMiniUser.setMoney(wxMiniUser.getMoney().subtract(wxMiniUserParam.getMoney()));
        wxMiniUserService.updateById(wxMiniUser);

        MoneyHistory moneyHistory = new MoneyHistory();
        moneyHistory.setMoney(wxMiniUserParam.getMoney());
        moneyHistory.setOpenId(wxMiniUserParam.getOpenId());
        moneyHistory.setType("4");
        moneyHistory.setCreateTime(new DateTime());
        moneyHistoryService.save(moneyHistory);

        map.put("wxMiniUser", wxMiniUser);
        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @PostMapping(value = "pay")
    public Object pay(WxMiniUserParam wxMiniUserParam) {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();

        WxMiniUser wxMiniUser = wxMiniUserService.getOne(new QueryWrapper<WxMiniUser>().eq("open_id", wxMiniUserParam.getOpenId()));
        wxMiniUser.setMoney(wxMiniUser.getMoney().add(wxMiniUserParam.getMoney()));
        wxMiniUserService.updateById(wxMiniUser);

        MoneyHistory moneyHistory = new MoneyHistory();
        moneyHistory.setMoney(wxMiniUserParam.getMoney());
        moneyHistory.setOpenId(wxMiniUserParam.getOpenId());
        moneyHistory.setType("1");
        moneyHistory.setCreateTime(new DateTime());
        moneyHistoryService.save(moneyHistory);

        map.put("wxMiniUser", wxMiniUser);
        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }
}


